package camid.test.testobjects;

import java.io.Serializable;

import camid.distribution.lifecycle.LifecycleScope;
import camid.distribution.lifecycle.LocationScope;
import camid.distribution.lifecycle.ManagedObject;
import camid.util.testobjects.DelayUtil;

@ManagedObject ( lifecycleScope = LifecycleScope.PER_CLIENT, 
	locationScope = LocationScope.CLOUD )
public class StatefulDelayObject implements IDelayObject, Serializable
{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3885894942500152639L;
	
	private long[] data;
	
	private int i;
	
	public StatefulDelayObject()
	{
		data = new long[32];
	}

	@Override
	public Long doDelay(Long delay) {
		
		delay = DelayUtil.gaussianDelay(delay);
//		while(endTime - startTime < delay)
//		{
//			try 
//			{
//				Thread.sleep(100);
//				endTime = System.currentTimeMillis();
//			} 
//			catch (InterruptedException e) 
//			{
//				e.printStackTrace();
//			}
//		}
		try {
			Thread.sleep(delay);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		data[i] = delay;
		i = (i + 1) % 32;
		return new Long(i);
	}
	
	
}
